﻿// ActionScript file
/**
 * 左、上、右、下的盒子容�?
 * 本类应该归于container目录�?
 * author	:com.nickro
 * 
 * USAGE	:
 * 			var block:Block=new Block();
 *			 block.addChild();
 * 			block.edge=new EdgeMetrics();
 * 
 * expands	:
 * 			1.设置外部背景接口
 * 			2.解决list的dataProvider与edge的问�?
 */
package org.nickro.controls{
	import flash.display.DisplayObject;
	
	import org.nickro.geom.EdgeMetrics;
	
	public class Block extends BaseControls{
		//private var _bg:*;
		
		private var _horizontalAlign:String="";
		private var _verticalAlign:String="";
		protected var _edge:EdgeMetrics=new EdgeMetrics();
		public function Block(dataProvider:Object=null){
			super(dataProvider);
		}

		/** 更新显示对象*/
		override protected function updateDislayObject():void{
			super.updateDislayObject();
			measure();
			
			var num:int=this.numChildren;
			var childs:Array=new Array;
			for(var i:int=0;i<num;i++){
				var child:DisplayObject=this.getChildAt(i);
				childs.push(child);
			}

			switch (_horizontalAlign){
				case "left":
					for(var i:int=0;i<num;i++){
						childs[i].x = 0;
					}
					break;
				case "center":
					for(var i:int=0;i<num;i++){
						childs[i].x = (this.width - childs[i].width)/2;
					}
					break;
				case "right":
					for(var i:int=0;i<num;i++){
						childs[i].x = this.width - childs[i].width;
					}
					break;
				case "":
					break;
			}
			
			switch (_verticalAlign){
				case "top":
					for(var i:int=0;i<num;i++){
						childs[i].y = 0;
					}
					break;
				case "middle":
					for(var i:int=0;i<num;i++){
						childs[i].y = (this.height - childs[i].height)/2;
					}
					break;
				case "bottom":
					for(var i:int=0;i<num;i++){
						childs[i].y = this.height- childs[i].height;
					}
					break;
					case "":
						break;
			}
			
			
			
		}
		
		/**
		 * 边界布局  注意:默认边界为[0,0,0,0]，如果不给edge赋值，此值将不会改变
		 */  
		
		protected function measure():void{
				this.x = _edge.left;
				this.y = _edge.top;
				//this.width = this.parent.width-_edge.right - _edge.left;
				//this.height = this.parent.height-_edge.bottom - _edge.top;
		}
		/**
		 * public getter/setter
		 */
		
		public function get horizontalAlign():String{
			return _horizontalAlign;
		}
		public function set horizontalAlign(value:String):void{
			_horizontalAlign=value;
			setChanged();
		}
		
		public function get verticalAlign():String{
			return _verticalAlign;
		}
		public function set verticalAlign(value:String):void{
			_verticalAlign=value;
			setChanged();
		}
		
		//边际的设�?
		public function set edge(value:EdgeMetrics):void{
			if(!(_edge.equals(value))){
				_edge=value;
				setChanged();
			}
			return;
		}
		public function get edge():EdgeMetrics{
			return _edge;
		}
	}
}